<?php


namespace App\Http\Controllers\App\Game\V1\Castle;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Models\GameDB\Castle\CastleDrawLog;
use App\Repositories\Game\Castle\CastleRepositories;
use App\Response\Response;
use App\Services\Tools\RedisService;
use Illuminate\Http\Request;

class CastleController extends BaseController
{
    /**
     * 宝石城堡 金币兑换宝石
     * @param Request $request
     * @return string
     * @throws BasicException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function exchange(Request $request)
    {
        $params = $request->only('number');
        $v = app('validator')->make(
            $request->all(),
            [
                'number' => "required|numeric|min:1",
            ],
            [
                'required' => ' :attribute必须输入',
                'numeric' => ' :attribute必须为数字'
            ],
            [
                'number' => '兑换数量'
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }
        $userId = (int)UID;
        $number = intval($params['number']);
        if ($number <= 0) {
            throw new BasicException(-1, '非法请求');
        }
        $data= CastleRepositories::getInstance()->exchange($userId, $number);
        // 返回蜡烛数量
        return Response::sendData($data);
    }

    /**
     * @return string
     * @throws BasicException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function info()
    {
        $userId = (int)UID;
        // 游戏info
        $gift = CastleRepositories::getInstance()->giftList($userId);
        //
        $redis = app('redis');
        $log_id = $redis->exists('castle:log:sn') ? $redis->get('castle:log:sn') : CastleDrawLog::query()->orderByDesc('id')->value('id');
        $time = $redis->get('castle:log:betting' . $log_id)??0;
        $setting    = json_decode(app("redis")->get("castle:setting"), true);
        if($gift['time_status'] == 1) {
            #下注倒计时时间
            $time   = $setting['bet_time'] - ($setting['sum_time'] - $time);
        }else if($gift['time_status'] == 3) {
            #结算倒计时时间
            $time   = $time - $setting['exit_time'];
        }
        return Response::sendData([
            'gift' => $gift['gift'],
            'sum' => $gift['sum'],
            'time_status' => $gift['time_status'],
            'content' => $gift['content'],
            'bet_count' => $gift['bet_count'],
            'user' => CastleRepositories::getInstance()->userInfo($userId),
            // 获取时间
            'time' => intval($time), // 后面补完 待开奖时间
            'log' => CastleRepositories::getInstance()->logList() // 最近10个开奖记录
        ]);
    }

    /**
     * @return string
     * @throws BasicException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface 获取兑换接口 信息
     */
    public function exchangeInfo()
    {
        return Response::sendData(
            CastleRepositories::getInstance()->exchangeList()
        );

    }

    /**
     *宝石城堡 开始押注
     * @param Request $request
     * @return string
     * @throws BasicException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * type  1 添加  2 减少
     */
    public function gameBet(Request $request)
    {
        $params = $request->only('number', 'gift_id', 'lattice', 'gift_info', 'type');
        $v = app('validator')->make(
            $request->all(),
            [
                'number' => "required|numeric",
                'gift_id' => "required|numeric",
                'lattice' => "required|numeric",
                'type' => "required|numeric",
            ],
            [
                'required' => ' :attribute必须输入',
                'numeric' => ' :attribute必须为数字'
            ],
            [
                'number' => '请选择数量',
                'gift_id' => '请选择当前礼物!',
                'lattice' => '请选择当前礼物',
                'type' => '请传入当前操作状态'
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }

        $number = intval($params['number']);
        if ($number <= 0) {
            throw new BasicException(-1, '非法请求');
        }
        $userId = (int)UID;
        return Response::sendData(CastleRepositories::getInstance()->gameBet($userId, $params));
    }

    /**
     *宝石城堡 往期
     */
    public function gameResult(Request $request)
    {
        $params = $request->only('day', 'perPage', 'page', 'type');
        $userId = (int)UID;
        return Response::sendData(CastleRepositories::getInstance()->gameResult($userId, $params['type'], $params['day'], $params['page'], $params['perPage']));
    }

    /**
     * @param Request $request
     * @return string
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws BasicException
     * 宝石城堡 中奖榜单，含日榜，周榜，月榜，总榜
     */
    public function drawRank(Request $request)
    {
        $userId = (int)UID;
        $params = $request->only('type', 'perPage', 'page');
        return Response::sendData(CastleRepositories::getInstance()->drawRank($userId, $params['type'], $params['page'], $params['perPage']));
    }

    /**
     *宝石城堡 历史历史中奖记录，直接走redis拿100局历史记录
     */
    public function drawLog(Request $request)
    {
        $userId = (int)UID;
        $params = $request->only('page', 'perPage', 'type');
        return Response::sendData(CastleRepositories::getInstance()->drawLog($userId, $params['type'], $params['page'], $params['perPage']));
    }

//    /**
//     *宝石城堡 我的历史中奖记录，page, limit
//     */
//    public function selfLog(Request $request)
//    {
//
//    }

    /**
     *宝石城堡 商店兑换
     * @throws \Exception
     */
    public function shopBuy(Request $request)
    {
        $userId = (int)UID;
        $params = $request->only('gift_id', 'shop_id', 'number');
        $v = app('validator')->make(
            $request->all(),
            [
                'gift_id' => "required|numeric",
                'shop_id' => "required|numeric",
                'number' => "required|numeric",
            ],
            [
                'required' => ' :attribute必须输入',
                'numeric' => ' :attribute必须为数字'
            ],
            [
                'gift_id' => '请传入兑换礼物id!',
                'shop_id' => '请传入商店id',
                'number' => '请传入数量',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10010, getValidateMessage($v->messages()));
        }
        $number = intval($params['number']);
        if ($number !== 1 || !$userId) {
            throw new BasicException(-1, '非法请求');
        }
        $coin_castle = CastleRepositories::getInstance()->shopBuy($userId, $params);
        return Response::sendData(['coin_castle' => $coin_castle], '兑换成功');
    }

    /**
     *宝石城堡 商店物品列表
     */
    public function shopItem(Request $request)
    {
        $params = $request->only('perPage', 'page');
        $userId = (int)UID;
        return Response::sendData(CastleRepositories::getInstance()->shopItem($userId, $params['page'], $params['perPage']));
    }


}
